// 基础配置
import wepy from 'wepy'
import configFile from '../config'
import API_DATA from './data'

const CONFIG = {
  host: configFile.PRO_API || configFile.DEV_API,
  path: 'mina_app/'
}

const BASEURL = CONFIG.host + '/' + CONFIG.path

import {
  connect
} from 'wepy-redux'
@connect({
  user(state) {
    return state.main.user
  }
})
class Api {
  get user () {
    const { computed } = this
    return computed && computed.user ? computed.user() : null
  }

  /**
   * 请求使用
   * @param {*} mdPath 中间件路径
   * @param {*} data 数据
   */
  POST = async (mdPath, data, loadingTips) => {
    if (loadingTips) {
      loadingTips = {
        title: loadingTips,
        mask: true
      }
    }
    wepy.showLoading(loadingTips)
    const response = await wepy.request({
      header: {
        'app-name': configFile.APP_NAME,
        'wx-union-user-id': this.user ? this.user.id : ''
      },
      url: BASEURL + mdPath,
      method: 'POST',
      data
    })
    wepy.hideLoading()
    let errTips = '网络错误'
    if (response.statusCode !== 200) {
      if (typeof response.data.result === 'string') {
        errTips = response.data.result
      }
      setTimeout(() => {
        wepy.showToast({
          title: errTips,
          icon: 'none',
          duration: 1500
        })
      }, 500)
      return false
    } else {
      if (response.data.code !== 200) {
        if (typeof response.data.result === 'string') {
          errTips = response.data.result
        }
        setTimeout(() => {
          wepy.showToast({
            title: errTips,
            icon: 'none',
            duration: 1500
          })
        }, 500)
        return false
      } else {
        if (response.data.tips) {
          wepy.showToast({
            title: response.data.tips,
            icon: 'none',
            duration: 1500
          })
        }
        return response.data
      }
    }
  }

  UPLOAD = (path, filePath, formData, url) => (
    new Promise((resolve, reject) => {
      wepy.showLoading({
        title: '请等待',
        mask: true
      })
      wx.uploadFile({
        url: url ? (url + path) : (BASEURL + path),
        filePath: filePath,
        name: 'file',
        formData: formData,
        success: res => {
          if (res.statusCode === 200) {
            const jsonData = JSON.parse(res.data)
            if (jsonData.code === 200) {
              if (jsonData.tips) {
                setTimeout(() => {
                  wepy.showToast({
                    title: jsonData.tips,
                    icon: 'none',
                    duration: 1500
                  })
                }, 1500)
              }
              resolve(jsonData)
            } else {
              if (typeof jsonData.result === 'string') {
                setTimeout(() => {
                  wepy.showToast({
                    title: jsonData.result,
                    icon: 'none',
                    duration: 1500
                  })
                }, 500)
              }
              resolve(false)
            }
          } else {
            reject(res)
          }
          wepy.hideLoading()
        },
        fail: err => {
          reject(err)
          wepy.hideLoading()
        }
      })
    })
  )

  apiData = () => {
    const { POST, GET, UPLOAD } = this

    const returnData = API_DATA({ POST, GET, UPLOAD })
    returnData.HOST = CONFIG.host
    return returnData
  }
}

export default (new Api).apiData()
